//
//  YDYCyclePhotoView.m
//  DY_Bilibili
//
//  Created by 杨德勇 on 2016/10/8.
//  Copyright © 2016年 杨德勇. All rights reserved.
//

#import "YDYCyclePhotoView.h"
#import <UIImageView+WebCache.h>
#import <UIButton+WebCache.h>
#import "YDYBanner.h"

@interface YDYCyclePhotoView()<UIScrollViewDelegate>
/**  */

@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
/** 页码控制器 */
@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;
/** frmame */
@property (nonatomic, assign) CGRect Photoframe;
/** timer */
@property (nonatomic, weak) NSTimer *timer;
@end

@implementation YDYCyclePhotoView


#pragma mark -
#pragma mark -setter
-(void)setPhotoUrlArray:(NSArray *)photoUrlArray{
    if (photoUrlArray.count == 0) return;
    _photoUrlArray = photoUrlArray;
    self.pageControl.numberOfPages = photoUrlArray.count;
    self.scrollView.contentSize = CGSizeMake(YDYScreenSize.width * photoUrlArray.count, 0);
    
    for (int i = 0; i < photoUrlArray.count; i++) {
        NSString *urlstring = photoUrlArray[i];
        
        UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(i * YDYScreenSize.width, 0, YDYScreenSize.width, 130)];
        [btn addTarget:self action:@selector(openUrl) forControlEvents:UIControlEventTouchUpInside];
        [btn sd_setImageWithURL:[NSURL URLWithString:urlstring] forState:UIControlStateNormal placeholderImage:self.placeHolderImage];
        [self.scrollView addSubview:btn];
        
    }
    
    [self startTimer];
    
}

-(void)setBanners:(NSArray<YDYBanner *> *)banners{
    _banners = banners;
    
    if (banners.count == 0) return;
    
    NSMutableArray *imgArray = [NSMutableArray arrayWithCapacity:4];
    for (YDYBanner *banner in banners) {
        [imgArray addObject:banner.img];
    }
    self.photoUrlArray = imgArray;
    
}
#pragma mark -
-(void)openUrl{
    
    YDYBanner *banner = self.banners[self.pageControl.currentPage];
    YDYLog(@"应该打开这个链接：%@",banner.link);
}


-(void)startTimer{
    //防止重复调用跟这个方法
    [self.timer invalidate];
    self.timer = nil;
    self.timer =  [NSTimer scheduledTimerWithTimeInterval:self.autoScrollTimeInterval target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
}

-(void)nextPage{
    NSInteger currentPage  = self.scrollView.contentOffset.x / self.bounds.size.width;
    self.pageControl.currentPage = currentPage;
    if (currentPage == self.photoUrlArray.count  - 1) {
        [self.scrollView setContentOffset:CGPointZero animated:YES];
        self.pageControl.currentPage = 0;
    }else{
        [self.scrollView setContentOffset:CGPointMake(self.scrollView.contentOffset.x + self.bounds.size.width, self.scrollView.contentOffset.y) animated:YES];
        self.pageControl.currentPage += 1;
    }
    
    
    
}


+(YDYCyclePhotoView *)ydyCyclePhotoView:(CGRect )frame placeHolderImage:(UIImage *)placeHolderImage autoScrollTime:(NSTimeInterval)time{
    YDYCyclePhotoView *cycleView =[[[NSBundle mainBundle]loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil]firstObject];
    cycleView.frame = frame;
    
    cycleView.placeHolderImage = placeHolderImage;
    cycleView.autoScrollTimeInterval = time;
    return cycleView;
}

#pragma mark -
#pragma mark -scrollview delegate
//-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
//    YDYLog(@"scrollViewDidScroll");
//}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    [self.timer invalidate];
    self.timer = nil;
    self.pageControl.currentPage = scrollView.contentOffset.x / YDYScreenSize.width;
    [self startTimer];
    
}

#pragma mark -
#pragma mark -lazy loading
-(NSTimeInterval)autoScrollTimeInterval{
    if (_autoScrollTimeInterval == 0) {
        _autoScrollTimeInterval = 3;
    }
    return _autoScrollTimeInterval;
}

@end
